/**
 * 列表公用的脚本
 */

/**
 * 下载二维码
 */
function download(ctx, path) {
    window.location.href = ctx + "/fileupload/download.do?filePath=" + path;
}

/**
 * 查询表单提交
 *
 * @param myForm
 */
function submitFunction(myForm) {
    // $(myForm).find("[name=timeType]").val("");
    $(myForm).submit();
}

function submitFunctionNew(myForm) {
    $(myForm).submit();
}

/**
 * 查询表单重置
 *
 * @param myForm
 */
function resetFunction(myForm) {
    $(myForm).find("input[type=text]").val("");
    $(myForm).find("input[type=number]").val("");
    $(myForm).find("select").find("option:eq(0)").prop("selected", true);
    $(myForm).find(".check-btn-inner").find("a").removeClass("active");
    $(myForm).find(".check-btn-inner").find("a:eq(0)").addClass("active")
    $(myForm).find(".check-btn-inner").find("[type=hidden]").val("");
    var isSuccess = $(myForm).find('#isSuccess').val();
    if (isSuccess != undefined) { //针对众筹查询的处理，isSuccess代表众筹项目的状态，置空表示查询全部
        $(myForm).find('#isSuccess').val('');
    }
}

/**
 * 二维码弹窗显示/隐藏
 *
 * @param container
 * @param obj
 */
function qrCodeDialog(container, clazz, obj, dialogName, type) {
    $(container).delegate(obj, 'click', function (e) {
        var $target = $(e.target);

        if ($target.hasClass(clazz)) {
            if (type == undefined) {
                var dialog = $target.closest(obj).siblings(dialogName);
                $(dialog).fadeIn();
            } else if (type == "2") {
                var dialog = $target.closest(container).find(dialogName);
                $(dialog).fadeIn();
            } else if (type == "3") {
                var dialog = $target.closest(obj).find(dialogName);
                $(dialog).fadeIn();
            }
        }
    });

    $(container).delegate(dialogName + '.close-icon', 'click', function (e) {
        $(this).closest('.f-def-dialog').fadeOut();
    });
}

/**
 * 例如“发布时间”块
 *
 * @param obj
 * @param value
 * @param myForm
 */
function setTimeType(obj, value, myForm) {
    $(obj).parent(".check-btn-inner").find("a").removeClass("active");
    $(obj).addClass("active");
    $(obj).parent(".check-btn-inner").find("[type=hidden]").val(value);
    $(myForm).find("[name=createStart]").val("");
    $(myForm).find("[name=createEnd]").val("");
    $(myForm).submit();
}

function showActive(startDate, endDate, container) {
    if (startDate != "" || endDate != "") {
        $(container).find("a").removeClass("active");
    }
}
function loadPageNew(opts) {
    opts = $.extend({
        elem: 'page_content',
        formName: 'myForm',
        totalCount: 0,
        limit: 10,
        page: 1,
    }, opts);
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: opts.elem,
            count: opts.totalCount,
            limit: opts.limit,
            layout: ['count', 'prev', 'page', 'next', 'skip'],
            curr: function () {
                return opts.page;
            }(),
            skip: true,
            theme: '#e8473f',
            jump: function (obj, first) {
                if (!first) {
                    $('#' + opts.formName).find("input[name=pageNo]").val(obj.curr);
                    if (typeof opts.callback === 'function') {
                        opts.callback.apply(this, arguments);
                    } else {
                        $('#' + opts.formName).submit();
                    }
                }
            }
        });
    });
}

/**
 * 查看弹窗
 *
 * @param title
 *            标题
 * @param url
 *            链接
 * @param width
 *            宽
 * @param height
 *            高
 * @param target
 *            对象
 */
function openDialogShow(title, url, width, height, target) {
    layer.open({
        type: 2,
        area: [width, height],
        title: title,
        maxmin: false, // 开启最大化最小化按钮
        content: url,
        btn: ['关闭'],
        yes: function (index, layero) {
            setTimeout(function () {
                top.layer.close(index);
            }, 100);// 延时0.1秒，对应360 7.1版本bug
        }
    });
}
function openDialogNoButton(title, url, width, height, target) {
    layer.open({
        type: 2,
        area: [width, height],
        title: title,
        maxmin: false, // 开启最大化最小化按钮
        content: url
    });
}

function openCerti(content, url, title, myForm) {
    layer.confirm(content, {
        icon: 3,
        title: '系统提示'
    }, function (index) {
        $.post(url, {}, function (data) {
            if (data.success == true) {
                util.layerMsgSuccess("认证成功", function () {
                    if (myForm == undefined) {
                        location.reload();
                    } else {
                        submitFunction(myForm);
                    }
                })
            } else {
                util.layerMsgError("认证失败");
            }
        });
    });
}
function copyObj(content, url, title, myForm) {
    layer.confirm(content, {
        icon: 3,
        title: '系统提示'
    }, function (index) {
        $.post(url, {}, function (data) {
            if (data.success == true) {
                util.layerMsgSuccess("复制成功", function () {
                    if (myForm == undefined) {
                        location.reload();
                    } else {
                        submitFunction(myForm);
                    }
                });
            } else {
                if (data.description != null) {
                    util.layerMsgError(data.description);
                } else {
                    util.layerMsgError("复制失败");
                }
            }
        });
    });
}